package com.saint.easeuitest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;

public class MainActivity extends AppCompatActivity {
    private EditText userId_et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userId_et = findViewById(R.id.user_id_et);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_chat_btn:
                startChat();
                break;

            default:
                break;
        }
    }

    private void startChat() {
        String chatId = userId_et.getText().toString().trim();
        if (!TextUtils.isEmpty(chatId)) {
            // 获取当前登录用户的 username
            String currUsername = EMClient.getInstance().getCurrentUser();
            if (chatId.equals(currUsername)) {
                Toast.makeText(this, "不能和自己聊天", Toast.LENGTH_SHORT).show();
                return;
            }
            // 跳转到聊天界面，开始聊天
            Intent intent = new Intent(this, ChatActivity.class);
            // EaseUI封装的聊天界面需要这两个参数，聊天者的username，以及聊天类型，单聊还是群聊
            intent.putExtra("userId", chatId);
            intent.putExtra("chatType", EMMessage.ChatType.Chat);
            startActivity(intent);
        } else {
            Toast.makeText(this, "Username 不能为空", Toast.LENGTH_LONG).show();
        }
    }
}
